package com.smbms.service;

import com.smbms.mapper.UserMapper;
import com.smbms.pojo.Role;
import com.smbms.pojo.User;
import com.smbms.util.PageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String userCode, String userPassword) {
        return userMapper.login(userCode,userPassword);
    }

    @Override
    public PageBean<User> getPage(Map<String,Object> map) {
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setCurrentPage((Integer) map.get("index"));
        pageBean.setPageSize((Integer) map.get("pageSize"));
        map.put("startIndex",pageBean.getStartIndex());
        //查询总记录数
        pageBean.setTotalCount(userMapper.getCount(map));
        //查询结果
        pageBean.setResult(userMapper.getUserList(map));

        return pageBean;
    }

    @Override
    public List<Role> getRoleList() {
        return userMapper.getRoleList();
    }

    @Override
    public User checkUserCode(String userCode) {
        return userMapper.checkUserCode(userCode);
    }

    @Transactional
    @Override
    public boolean userAddSave(User user) {
        if (userMapper.userAddSave(user) > 0){
            return true;
        }
        return false;
    }

    @Override
    public User getUser(Integer userId) {
        return userMapper.userView(userId);
    }

    @Override
    public boolean modifyUser(User user) {
        if (userMapper.modifyUser(user) > 0){
            return true;
        }
        return false;
    }

    @Override
    public int deleteUser(Integer userId) {
        return userMapper.deleteUser(userId);
    }
}
